
// const updateManager = wx.getUpdateManager();
// if (updateManager) {
//   updateManager.onCheckForUpdate(function (res) {
//     // 请求完新版本信息的回调
//   })
//   updateManager.onUpdateReady(function () {
//     wx.showModal({
//       title: '更新提示',
//       content: '新版本已经准备好，是否重启应用？',
//       success: function (res) {
//         if (res.confirm) {
//           // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
//           updateManager.applyUpdate();
//         }
//       }
//     })
//   })
// }

var login = require('/utils/login.js');
var util = require('/utils/util.js');
var settings = require('/utils/settings.js');
import {
  request
} from '/utils/request.js'

App({
  resthttp: request,
  login: login.login,
  onLaunch: function (e) {
    console.log('app.js onLaunch ======', e);
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    let storeId = extConfig.StoreId;
    that.globalData.storeId = storeId;
    that.globalData.storeAppId = extConfig.StoreAppId;
    that.authUpdate();  //检查更新
    util.getMobileSysInfo(function (data) {
      that.globalData.platform = data
    })
    that.onLineCallback = () => {
      let cardId = wx.getStorageSync('cardId');
      let source = 1;
      let shareUid = 0;
      let uid = 0;
      if (!cardId) {
        cardId = 0;
      }
      let params = decodeURIComponent(e.query.scene);
      if (params && params != 'undefined') {//扫描小程序二维码进入
        let plist = '';
        let type = 0;
        if (params.indexOf('p-') != -1) {
          let param = params.match(/p-(\S*)/)[1];//参数
          plist = param.toString().split('_');
          if (params.indexOf('t-') != -1) {
            type = params.match(/t-(\S*)\*p/)[1];//路由
          }
        } else {
          let param = params.match(/p=(\S*)/)[1];//参数
          plist = param.toString().split('#');
          if (params.indexOf('t=') != -1) {
            type = params.match(/t=(\S*)&p/)[1];//路由
          }
        }
        if (type == 3 || type == 4 || type == 7 || type == 9 || type == 10) {
          cardId = plist[0];          
        }
        if (type == 3) {
          source = 4;   //股东客户
          shareUid = plist[1];
        }
        if (type == 11) {//联盟码
          cardId = plist[0];
          source = 5;
          if (plist[2]) {
            shareUid = plist[2];
          }
        }
      } else {
        if (e.query.id) {
          cardId = e.query.id;
        }
        if (e.query.source) {
          source = e.query.source;
        }
        if (e.path.indexOf('live-player-plugin') != -1) {
          source = 6;
        }
        if (e.query.holderid) {
          shareUid = e.query.holderid;
        }
        if (e.query.shareUid) {
          shareUid = e.query.shareUid;
        }
      }
      if (that.globalData.userInfo.Uid) {
        uid = that.globalData.userInfo.Uid;
      }
      console.log('shareUid',shareUid)
      if (cardId > 0 && uid > 0) {
        wx.request({
          url: that.globalData.host + '/api/BusinessCard/BindClient?staffId=' + cardId + '&uid=' + uid + '&source=' + source + '&shareUid=' + shareUid,
          success: function (res) {
            console.log("进入小程序建立关系成功", cardId);
          },
        })
      }
    }
  },

  onShow: function (e) {
    let that = this;
    that.getSysInfo();
    console.log('app.js onshow ======', e);
    if (e.scene != 1154) {//朋友圈分享不登陆
      login.login(this);
    }
    if (e.scene == 1154) {//朋友圈打开单页模式
      that.globalData.isFriendState = 1;
      that.globalData.friendUid = e.query.uid;
    }
    if (e.scene == 1155) {//单页模式打开小程序
      that.globalData.isFriendState = 0;
    }
    util.authori(e);
  },

  getSysInfo: function () {
    let that = this;
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuLeft = menuButtonInfo.left;
    that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    that.globalData.menuWidth = menuButtonInfo.width;
    that.globalData.menuHeight = menuButtonInfo.height;
    if (that.globalData.navBarHeight == 44) {
      that.getSysInfo();
    }
  },

  onHide: function () {
    var that = this;
    settings.clear_user_coupon_dialog_status();
    that.globalData.diningSid = 0;
  },

  authUpdate: function(){
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
      })
      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          }
        })
      })
    }
  },
  globalData: {
    //用户头像
    userInfo: null,
    myUserInfo: null,
    // 微街秀appid：wx310b2251cedf33e0
    // 玖玖客appid：wx9ec3c9261a80753d
    //正式环境注意地址中含有特定店铺ID，请更换
    storeId: 0,
    storeAppId: 0,
    expireTime: '',//名片过期时间
    loginCount: 0,
    versions: 'v7.3.26',
    platform: '', //客户端平台
    prepayId: '',
    diningSid: 0,//餐位Id
    isFriendState: 0,//1朋友圈分享
    friendUid: 0,
    navBarHeight: 0, // 导航栏高度
    menuLeft: 0,
    menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
    menuBotton: 0, // 胶囊距底部间距（保持底部间距一致）
    menuWidth: 0,
    menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
    host: 'https://weapp-slave.711688.net.cn',//微街秀线上测试域名
    // host: 'https://weapp.711688.net.cn',//玖玖客正式域名
    webhost: 'https://weijieke.711688.net.cn/admin_wjk2/pages/', //网页跳传域名
    webhost2: 'https://weijieke.711688.net.cn/admin_wjk2/', //网页跳传域名
    webhost3: 'https://weijieke.711688.net.cn/QDChat/pages/', //网页跳传域名
    socketUrl: 'wss://im-socket.711688.net.cn/wss',
    staff:{Id:0,Uid: 0,IndustryCode: 0}, //当前名片
    jjCard: {Id: 2602, Name:'玖玖卡', CardType: 4, Uid: 1362989}  //玖玖卡
  }
})